<?php
/**
 * Description of DayIntervalForm
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 */

class DayIntervalForm extends sfForm {

    public function configure() {
        $this->setWidgets(array(
            'time_start_hour' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeHourChoices())),
            'time_start_minute' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeMinuteChoices())),
            'time_end_hour' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeHourChoices())),
            'time_end_minute' => new sfWidgetFormSelect(array('choices' => Valuelist::getTimeMinuteChoices())),
            'name' => new sfWidgetFormInput(),
            'seats' => new sfWidgetFormInput(),
        ));

        $this->setValidators(array(
            'time_start_hour' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'time_start_minute' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'time_end_hour' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'time_end_minute' => new sfValidatorString(array('min_length' => 2, 'max_length' => 2, 'required' => true)),
            'name' => new sfValidatorString(array('max_length' => 20, 'min_length' => 3, 'required' => true)),
            'seats' => new sfValidatorInteger(array('min' => 1, 'max' => 50, 'required' => true)),
        ));

        $this->validatorSchema->setPostValidator(
                    new sfValidatorCallback(array('callback' => array($this, 'checkTimes'))));

        $this->widgetSchema->setNameFormat('day_interval[%s]');
    }


    public function checkTimes($validator, $values) {

        Util::loadHelper('I18N');
        
        if ($values['time_end_hour'] < $values['time_start_hour'])
            throw new sfValidatorError($validator, __('Not a valid timespan')); // <----------- add i18n on error

        if ($values['time_start_hour'] == $values['time_end_hour'])
            if ($values['time_end_minute'] <= $values['time_start_minute'])
                throw new sfValidatorError($validator, __('Not a valid timespan')); // <------------ add i18n on error

        return $values;
    }
}
